(function (vc, vm) {

    vc.extends({
        data: {
            editPartnerPoolInfo: {
                ppId: '',
                partnerName: '',
                partnerTypes: [],
                partnerType: '',
                certPath: '',
                state: '',
                remark: '',
                partnerKeys: [],
            }
        },
        _initMethod: function () {
            $that._loadEditPartnerAdapt();
        },
        _initEvent: function () {
            vc.on('editPartnerPool', 'openEditPartnerPoolModal', function (_params) {
                $that.refreshEditPartnerPoolInfo();
                $('#editPartnerPoolModel').modal('show');
                vc.copyObject(_params, $that.editPartnerPoolInfo);
                $that._editChangeToType();

            });

        },
        methods: {
            editPartnerPoolValidate: function () {
                return vc.validate.validate({
                    editPartnerPoolInfo: $that.editPartnerPoolInfo
                }, {
                    'editPartnerPoolInfo.partnerName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "名称不能超过64"
                        },
                    ],
                    'editPartnerPoolInfo.partnerType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "合作商不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "合作商不能超过64"
                        },
                    ],
                    'editPartnerPoolInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "使用说明不能超过512"
                        },
                    ],
                    'editPartnerPoolInfo.ppId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "编号不能为空"
                        }]

                });
            },
            editPartnerPool: function () {
                if (!$that.editPartnerPoolValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.editPartnerPoolInfo.shopId = vc.getCurrentCommunity().shopId;

                vc.http.apiPost(
                    '/partner.updatePartnerPool',
                    JSON.stringify($that.editPartnerPoolInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editPartnerPoolModel').modal('hide');
                            vc.emit('partnerPool', 'listPartnerPool', {});
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);
                    });
            },
            refreshEditPartnerPoolInfo: function () {
                let _partnerTypes = $that.editPartnerPoolInfo.partnerTypes;
                $that.editPartnerPoolInfo = {
                    ppId: '',
                    partnerName: '',
                    partnerType: '',
                    partnerTypes: _partnerTypes,
                    certPath: '',
                    state: '',
                    remark: '',
                    partnerKeys: [],
                }
            },
            _editChangeToType: function () {
                if (!$that.editPartnerPoolInfo.partnerType) {
                    return;
                }
                let _param = {
                    params: {
                        partnerType: $that.editPartnerPoolInfo.partnerType,
                        page: 1,

                        row: 100
                    }
                }
                //发送get请求
                vc.http.apiGet('/partner.listPartnerKey',
                    _param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        _json.data.forEach(item => {
                            item.columnValue = '';
                        })
                        $that.editPartnerPoolInfo.partnerKeys = _json.data;
                        //todo 查询 支付信息
                        $that._editLoadPartnerData();
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            
            _loadEditPartnerAdapt:function(){
                let param = {
                    params: {
                        page: 1,
                        row: 100,
                    }
                };
                //发送get请求
                vc.http.apiGet('/partner.listPartnerAdapt', param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.editPartnerPoolInfo.partnerTypes = _json.data;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _editLoadPartnerData: function () {
                let param = {
                    params: {
                        page: 1,
                        row: 1,
                        shopId: vc.getCurrentCommunity().shopId,
                        ppId: $that.editPartnerPoolInfo.ppId
                    }
                };
                $that.editPartnerPool.configIds = [];
                //发送get请求
                vc.http.apiGet('/partner.listPartnerPool',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        let _values = _json.data[0].values;
                        _values.forEach(_value => {
                            $that.editPartnerPoolInfo.partnerKeys.forEach(_key =>{
                                if(_value.columnKey == _key.columnKey){
                                    _key.columnValue = _value.columnValue;
                                }
                            })
                        });

                        let _configs = _json.data[0].configs;
                        if(!_configs){
                            return;
                        }
                        _configs.forEach(_config =>{
                            $that.editPartnerPoolInfo.configIds.push(_config.configId);
                        })

                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            }
        }
    });

})(window.vc, window.$that);
